﻿#include "CScreen.h"
using namespace DarkStorm2D;
using namespace DarkStorm2D::GraphicsCore;

bool CScreen::m_IsInit;
CColor CScreen::m_ClearColor;
int CScreen::m_LastFPS;

CScreen::CScreen()
{
	m_IsInit = false;
	
	m_ClearColor.SetColor(0,0,0);
	m_LastFPS = 0;
}


CScreen::~CScreen()
{
	m_IsInit = false;
	m_ClearColor.SetColor(0,0,0);
	m_LastFPS = 0;
}

bool CScreen::Initialize(int Width, int Height, bool FullScreen, CColor * ClearColor)
{	
	if(!IsInit())
	{
		if (FullScreen)
		{
			int tmpFlags = al_get_new_display_flags();
			al_set_new_display_option(ALLEGRO_OPENGL,2,ALLEGRO_REQUIRE);
			al_set_new_display_flags(ALLEGRO_FULLSCREEN);
			al_create_display(Width,Height);
			al_set_new_display_flags(tmpFlags);
		}
		else
		{ 
			if (NULL == al_create_display(Width,Height))
				return false;
		}

		if (NULL == ClearColor)
		{
			m_IsInit = true;
			return true;
		}
		else 
		{
			CScreen::SetClearColor(ClearColor);

			m_IsInit = true;
			return true;
		}
	}
	return false;
}

void CScreen::Shoutdown()
{
	al_destroy_display(al_get_current_display());
	m_IsInit = false;
}

bool CScreen::IsInit()
{
	return m_IsInit;
}

void CScreen::SetClearColor(CColor * ClearColor)
{
	memcpy(&m_ClearColor, ClearColor, sizeof(CColor));
}


void CScreen::BeginFrame()
{
	GetSingleton().m_Timer.Update();
}

void CScreen::Clear()
{
	al_clear_to_color(m_ClearColor.m_Color);
}

void CScreen::EndFrame()
{

	al_flip_display();

	// Oblicz ilość klatek na sekunde
	GetSingleton().m_Timer.Update();
	m_LastFPS = (int) (1 / GetSingleton().m_Timer.GetDelta());
}